/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *   g.latarowski@gmail.com                                                *
 *   maciej.gajewski0@gmail.com                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/




#include "gexception.h"
#include "gxmlelement.h"

const char * VARIANT_TAG       = "variant";
const char * VARIANT_TYPE_TAG  = "type";
const char * VARIANT_VALUE_TAG = "value";



// ===========================================================================
//                               GXmlElement
// ===========================================================================


// ----------------- GXmlElement::variantToXml() -----------------------------
QDomElement GXmlElement::variantToXml( QDomDocument & doc,
									   const QVariant & value
									 )
{
		// create variant element 
	QDomElement variantElement = doc.createElement( VARIANT_TAG );
		// append type
	QDomElement typeElement = doc.createElement( VARIANT_TYPE_TAG );
	variantElement.appendChild( typeElement );
	QDomText typeText = doc.createTextNode( value.typeName() );
	typeElement.appendChild( typeText );
		// append variant's value
	QDomElement valueElement = doc.createElement( VARIANT_VALUE_TAG );
	variantElement.appendChild( valueElement );
	QDomText valueText = doc.createTextNode( value.toString() );
	valueElement.appendChild( valueText );

		// return created element 
	return variantElement;
}
// ---------------------------------------------------------------------------


// ---------------- GXmlElement::variantFromXml() ----------------------------
QVariant GXmlElement::variantFromXml( QDomElement & domElement )
{
	QDomElement variantElement;
		// check if searched node isn't root 
	if( domElement.tagName() == VARIANT_TAG )
	{
		variantElement = domElement;
	}
	else
	{
		variantElement = domElement.firstChildElement( VARIANT_TAG );
	}
		// if there is no valueName element then throw an error
	if( variantElement.isNull() )
	{
		return QVariant();
	}

		// parse type
	QDomElement typeElement;
	typeElement = variantElement.firstChildElement( VARIANT_TYPE_TAG );
	if( typeElement.isNull() )
	{
		return QVariant();
	}
		// parse value
	QDomElement valueElement;
	valueElement = variantElement.firstChildElement( VARIANT_VALUE_TAG );
	if( valueElement.isNull() )
	{
		return QVariant();
	}

	// ------------------------------------------------
	// parse variant

	QVariant::Type type = QVariant::nameToType( typeElement.text().toAscii() );
	if( type == QVariant::Invalid )
	{
		return QVariant();
	}
		// prepare QVariant with value
	QVariant value( valueElement.text() );
		// can convert ?
	if( !value.canConvert( type ) )
	{
		return QVariant();
	}
		// convert into proper type
	value.convert( type );
	
	return value;
}
// ---------------------------------------------------------------------------




// eof
